<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="list"
      :rules="formRules"
      label-width="0px"
      v-loading="formLoading"
    >
  <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="科室名称" align="center" prop="deptName" />
        <el-table-column label="订单编号" align="center" prop="orderId" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="数量" min-width="100">
          <template #default="{ row,$index }">
            <el-form-item :prop="`${$index}.countActual`" :rules="formRules.countActual" class="mb-0px!">
              <el-input v-model="row.countActual" placeholder="请输入数量" />
            </el-form-item>
          </template>
      </el-table-column>
      </el-table>
    </ContentWrap>
  </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>  
</template>
<script setup lang="ts">
import { DepPurchaseApi } from '@/api/erp/deppurchase'
import { DepPurchaseItemApi,DepPurchaseItemVO } from '@/api/erp/deppurchaseitem'


const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const queryParams = reactive({
  integrateId: undefined,
  productUnitId: undefined,
  productStandards: undefined,
  supplierId: undefined,
  productName: undefined
})
const formData = ref({
  id: undefined, // 编号
  orderId: undefined, // 科室采购订单编号
  supplierId: undefined ,// 供应商编号
  productId: undefined, // 产品编号
  productUnit: undefined, // 产品单位
  productStandards: undefined, // 产品规格
  productPrice: undefined, // 产品单价
  count: undefined, // 数量
  totalPrice: undefined, // 总价
  remark: undefined, // 备注
  productName: undefined, // 产品名称
  list:undefined
})
const formRules = reactive({
  countActual: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
})

const formRef = ref() // 表单 Ref

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    list.value = await DepPurchaseApi.getDepPurchaseItemListByOrderId(props.orderId)
  } finally {
    loading.value = false
  }
}

/** 打开弹窗 */
const open1 = async (type: string, row: any, integrateId: String) => {
  dialogVisible.value = true
  dialogTitle.value = '详情'
  queryParams.integrateId = integrateId
  queryParams.productUnitId = row.productUnitId
  queryParams.productStandards = row.productStandards
  queryParams.supplierId = row.supplierId
  queryParams.productName = row.productName
  // 修改时，设置数据
    formLoading.value = true
    try {
      list.value = await DepPurchaseApi.getDepPurchaseItemList(queryParams)
    } finally {
      formLoading.value = false
    }
  }

  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()

  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DepPurchaseItemVO
    data.list = list.value
    await DepPurchaseItemApi.updateDepPurchaseItemByList(data)
    message.success(t('common.updateSuccess'))
    
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

defineExpose({ open1 }) // 提供 open 方法，用于打开弹窗

/** 初始化 **/
// onMounted(() => {
//   getList()
// })
</script>